<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/28 0028
 * Time: 下午 3:01
 */

namespace app\sports_admin\service;
use app\exception\ConstitutionProjectException;
use app\sports_admin\model\ConstitutionProject as ConstitutionProjectModel;
use app\sports_admin\model\ConstitutionType as ConstitutionTypeModel;
use app\sports_admin\model\ConstitutionType;
use think\Db;

class ConstitutionProject extends Base
{
    /**
     * 项目列表
     */
    public static function projectList()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        //页数
        $params['page'] = intval($params['page']);
        //条数
        $params['rows'] = intval($params['rows']);
        $info = ConstitutionProjectModel::projectList($params);
        return json($info);
    }
    /**
     * 添加项目
     */
    public static function insert()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        for ($i=0;$i<$params['LearnTRLastIndex'];$i++){
            $info[$i]['type_id'] = $params['type_id'];
            $info[$i]['grade_id'] = $params['grade_id'];
            $info[$i]['sex'] = $params['sex'];
            $info[$i]['year'] = $params['year'];
            $info[$i]['low_score'] = $params['low_score'.$i];
            $info[$i]['high_score'] = $params['high_score'.$i];
            $info[$i]['score'] = $params['score'.$i];
        }
        $bool = (new ConstitutionProjectModel())->saveAll($info);
        if(!$bool){
            throw new ConstitutionProjectException(['msg'=>'添加失败','errorCode'=>400]);
        }
        return json(['msg'=>'添加成功','code'=>200]);
    }

    /**
     * 修改
     * @return \think\response\Json
     * @throws ConstitutionProjectException
     */
    public static function update()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        Db::startTrans();
        $bool = ConstitutionProjectModel::destroy(function ($query)use($params){
            $query->where('type_id',$params['old_type_id'])->where('grade_id',$params['old_grade_id'])->where('year',$params['old_year'])->where('sex',$params['old_sex']);
        });
        if(!$bool){
            Db::rollback();
            throw new ConstitutionProjectException(['msg'=>'网络异常请稍后重试','errorCode'=>400]);
        }
        for ($i=0;$i<$params['LearnTRLastIndex'];$i++){
            $info[$i]['type_id'] = $params['type_id'];
            $info[$i]['grade_id'] = $params['grade_id'];
            $info[$i]['sex'] = $params['sex'];
            $info[$i]['year'] = $params['year'];
            $info[$i]['low_score'] = $params['low_score'.$i];
            $info[$i]['high_score'] = $params['high_score'.$i];
            $info[$i]['score'] = $params['score'.$i];
        }
        $bool = (new ConstitutionProjectModel())->saveAll($info);
        if(!$bool){
            Db::rollback();
            throw new ConstitutionProjectException(['msg'=>'修改失败','errorCode'=>400]);
        }
        Db::commit();
        return json(['msg'=>'修改成功','code'=>200]);
    }
    /**
     * 删除
     * @return \think\response\Json
     * @throws ConstitutionProjectException
     */
    public static function delete()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        $bool = ConstitutionProjectModel::destroy(function ($query)use($params){
            $query->where('type_id',$params['type_id'])->where('grade_id',$params['grade_id'])->where('year',$params['year'])->where('sex',$params['sex']);
        });
        if(!$bool){
            throw new ConstitutionProjectException(['msg'=>'删除失败','errorCode'=>400]);
        }
        return json(['msg'=>'删除成功','code'=>200]);
    }
    /**
     * 类别列表
     */
    public static function typeList()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        //页数
        $params['page'] = intval($params['page']);
        //条数
        $params['rows'] = intval($params['rows']);
        $info = ConstitutionTypeModel::typeList($params);
        return json($info);
    }
    /**
     * 类别添加
     */
    public static function typeInsert()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        $bool = ConstitutionTypeModel::create([
            'title'=>$params['title'],'grade_id'=>','.implode(',',$params['grade_id']).',','unit'=>$params['unit']
        ]);
        if(!$bool){
            throw new ConstitutionProjectException(['msg'=>'添加失败','errorCode'=>400]);
        }
        return json(['msg'=>'添加成功','code'=>400]);
    }
    /**
     * 类别修改
     */
    public static function typeUpdate()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        $bool = (new ConstitutionTypeModel())->save(['title'=>$params['title'],'grade_id'=>','.implode(',',$params['grade_id']).',','unit'=>$params['unit']],['id'=>$params['id']]);
        if(!$bool){
            throw new ConstitutionProjectException(['msg'=>'修改失败','errorCode'=>400]);
        }
        return json(['msg'=>'修改成功','code'=>400]);
    }
    /**
     * 删除类别
     * @return \think\response\Json
     * @throws ConstitutionProjectException
     */
    public static function typeDelete()
    {
        //获取参数
        $params = self::getHttpRequestPrograms();
        $params = $params->param();
        $bool = ConstitutionTypeModel::destroy($params['id']);
        if(!$bool){
            throw new ConstitutionProjectException(['msg'=>'删除失败','errorCode'=>400]);
        }
        return json(['msg'=>'删除成功','code'=>200]);
    }
}